package com.hdax.dm.controller;

import com.hdax.dm.common.exception.order.OrderException;
import com.hdax.dm.common.exception.user.UserLoginException;
import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.entity.user.User;
import com.hdax.dm.service.OrderService;
import com.hdax.dm.vo.OrderVo;
import com.hdax.dm.vo.confirmpayVo;
import com.hdax.dm.vo.queryorderlistVo;
import com.hdax.dm.vo.querypersoninfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    //生成新的订单和修改座位状态,返回订单编号
    @PostMapping(path = "/submitorder")
    public CommonResponse<String> saveOrder(@RequestBody OrderVo ordervo, HttpServletRequest request) throws OrderException{
        return DtoUtil.returnSuccess("",orderService.saveOrder(ordervo,request));
    }

    //根据订单号查询订单信息
    @PostMapping(path = "/confirmpay")
    public CommonResponse<confirmpayVo> confirmpay(@RequestBody Map<String,String> params){
        return DtoUtil.returnSuccess("",orderService.confirmpay(params.get("orderNo")));
    }

    //根据条件查询订单信息
    @PostMapping(path = "/queryorderlist")
    public CommonResponse<List<queryorderlistVo>> queryorderlist(@RequestBody Map<String,Object> params, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null || token.equals("")){
            throw new UserLoginException(UserLoginException.ERR_INFO.NO_LOGIN_MSG,
                    UserLoginException.ERR_INFO.NO_LOGIN_CODE);
        }
        String s = token.split(":")[1].split("-")[0];
        params.put("userId",s);
        return DtoUtil.returnSuccess("",orderService.queryorderlist(params));
    }

    //根据条件查询订单信息
    @PostMapping(path = "/deleteticketbuyer")
    public CommonResponse<String> deleteticketbuyer(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",orderService.deleteticketbuyer(params.get("linkId")));
    }

    //查询个人信息
    @PostMapping(path = "/querypersoninfo")
    public CommonResponse<querypersoninfoVo> querypersoninfo(@RequestBody Map<String,Long> params, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null || token.equals("")){
            throw new UserLoginException(UserLoginException.ERR_INFO.NO_LOGIN_MSG,
                    UserLoginException.ERR_INFO.NO_LOGIN_CODE);
        }
        String s = token.split(":")[1].split("-")[0];
        return DtoUtil.returnSuccess("",orderService.querypersoninfo(Long.parseLong(s)));
    }

    //修改个人信息
    @PostMapping(path = "/modifypersoninfo")
    public CommonResponse<String> modifypersoninfo(@RequestBody Map<String,String> params, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null || token.equals("")){
            throw new UserLoginException(UserLoginException.ERR_INFO.NO_LOGIN_MSG,
                    UserLoginException.ERR_INFO.NO_LOGIN_CODE);
        }
        String s = token.split(":")[1].split("-")[0];
        params.put("id",s);
        boolean flag = orderService.modifypersoninfo(params);
        return DtoUtil.returnSuccess("",flag == true?"修改成功!":"修改失败!");
    }

    @ExceptionHandler(value = {OrderException.class,UserLoginException.class})
    public CommonResponse exceptionHandler(OrderException orderException){
        return DtoUtil.returnFail(orderException);
    }

}
